package com.yejiarong.weather.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author YeJR
 */
@Configuration
public class SecurityConfig {

    private final AuthConfig authConfig;

    public SecurityConfig(AuthConfig authConfig) {
        this.authConfig = authConfig;
    }


    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(authorize ->
                        authorize
                                .requestMatchers(authConfig.getPermit().getEndpoints()).authenticated()
                                .anyRequest().authenticated()
                )
                .oauth2ResourceServer(oauth2 -> oauth2
                        .jwt(jwt -> jwt
                                .jwkSetUri(authConfig.getJwkUri())
                        )
                );

        return http.build();
    }
}
